import './style.css';

import Map from 'ol/Map.js';
import OSM from 'ol/source/OSM.js';
import TileLayer from 'ol/layer/Tile.js';
import View from 'ol/View.js';
import {getBottomLeft, getTopRight} from 'ol/extent.js';
import {toLonLat} from 'ol/proj.js';

const map = new Map({
  layers: [
    new TileLayer({
      source: new OSM(),
    }),
  ],
  target: 'map',
  view: new View({
    center: [1299640.31914674, 3400234.733893836],
    zoom: 6,
  }),
});
map.on('moveend', onMoveEnd);

function onMoveEnd(evt) {
  let map = evt.map;
  let extent = map.getView().calculateExtent(map.getSize());
  let bottomLeft = toLonLat(getBottomLeft(extent));
  let topRight = toLonLat(getTopRight(extent));
  let center = map.getView().getCenter();
  display('left', wrapLon(bottomLeft[0]));
  display('bottom', bottomLeft[1]);
  display('right', wrapLon(topRight[0]));
  display('top', topRight[1]);
  display("center", toLonLat(center));
}

function display(id, value) {
  console.log(id, value);
}

function wrapLon(value) {
  const worlds = Math.floor((value + 180) / 360);
  return value - worlds * 360;
}
